package sk.lieskove.jianghongtiao.paris.core;

import sk.lieskove.jianghongtiao.common.exceptions.InvalidPropertiesSettingsException;
import sk.lieskove.jianghongtiao.commonpersist.Persist;
import sk.lieskove.jianghongtiao.commonpersist.Serialize;
import sk.lieskove.jianghongtiao.websearch.persistence.ClassificationStorage;
import sk.lieskove.jianghongtiao.websearch.enums.DocumentType;
import sk.lieskove.jianghongtiao.paris.core.exceptions.WrongNumberOfArguments;
import sk.lieskove.jianghongtiao.websearch.FullWebSearch;
import sk.lieskove.jianghongtiao.websearch.WebSearch;
import sk.lieskove.jianghongtiao.websearch.service.SearchQuery;
import sk.lieskove.jianghongtiao.websearch.service.SearchQueryImpl;
import sk.lieskove.jianghongtiao.websearch.persistence.WebSearchResponse;
import sk.lieskove.jianghongtiao.websearch.service.searchengine.NoWebSearchAllowedException;
import sk.lieskove.jianghongtiao.common.enums.searchserviceenum.SearchServiceEnum;

import java.io.File;
import java.io.IOException;
import java.net.SocketException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args ) throws NoWebSearchAllowedException, 
            InvalidPropertiesSettingsException, 
            SocketException, 
            IOException, 
            WrongNumberOfArguments {
        
        //emerging patterns!
        //KD nuggets
        //review projektov z minuleho roka (RED)
        //
        
        
//        Object deserialize = Serialize.deserialize(new File("search.obj"), ArrayList.class);
//        ArrayList<WebSearchResponse> results = (ArrayList<WebSearchResponse>) deserialize;
//        
//        TestPreprocessManager pm = new TestPreprocessManager();
//        for (WebSearchResponse wsr : results) {
////            if(wsr.isAdequateDocument()){
////                pm.preprocess(wsr);
////            } else {
////                throw new IllegalStateException("Some items are not used any more"
////                        + ", but they are still there");
////            }
//            System.out.println(wsr);
//        }
//        
//        //printResults(deserialize);
//        
//        if(true)return;
        Long start = System.currentTimeMillis();
        WebSearch es = new FullWebSearch();
        SearchQuery query = new SearchQueryImpl();
//        query.addWord(readFileToString);
//        query.addWord("Katarína Janigáčová");
        query.addWord("Martin Jurčo");
//        ArrayList<WebSearchResponse> searchAll = 
//                (ArrayList<WebSearchResponse>) es.searchAll(query);
        ArrayList<WebSearchResponse> searchAll = 
                (ArrayList<WebSearchResponse>) es.search(query, 
                SearchServiceEnum.WEBSEARCH_BING,
                SearchServiceEnum.WEBSEARCH_GOOGLE);
//        ArrayList<WebSearchResponse> searchAll = 
//                (ArrayList<WebSearchResponse>) es.search(query, 
//                                SearchServiceEnum.ARES_BASIC,
//                SearchServiceEnum.ARES_CHURCHES_AND_RELIGIOUS_SOCIETIES,
//                SearchServiceEnum.ARES_CIVIC_ASSOCIATIONS_GUILDS_CLUBS,
//                SearchServiceEnum.ARES_COMMERCIAL_REGISTER,
//                SearchServiceEnum.ARES_COMMON_AGRICULTURAL_REGISTER,
//                SearchServiceEnum.ARES_ECONOMIC_ENTITIES,
//                SearchServiceEnum.ARES_FOREIGN_EXCHANGE_SPOTS_AND_LICENCES,
//                SearchServiceEnum.ARES_INSOLVENT_REGISTER,
//                SearchServiceEnum.ARES_INSURANCE_INTERMEDIARIES_LOSS_ADJUSTERS,
//                SearchServiceEnum.ARES_POLITICAL_PARTIES_MOVEMENTS,
//                SearchServiceEnum.ARES_REGISTER_ECONOMIC_ENTITIES_STATISTICAL_OFFICE,
//                SearchServiceEnum.ARES_REGISTER_OF_BANKRUPTS,
//                SearchServiceEnum.ARES_REGISTER_OF_HEALTH,
//                SearchServiceEnum.ARES_REGISTRY,
//                SearchServiceEnum.ARES_SCHOOL_REGISTER,
//                SearchServiceEnum.ARES_STANDARD,
//                SearchServiceEnum.ARES_TRADE_REGISTER);
        //serialize results for future usage
        File f = new File("search.obj");
        Serialize.serialize(f, searchAll);
        System.out.println("Saved to file: "+f.getAbsolutePath());
        persistResults(searchAll);
        printResults(searchAll);
        Timestamp time = new Timestamp(System.currentTimeMillis() - start);
        System.out.println("Running time: " + time.toString());
        
        //System.in.read();
    }
    
    private static void printResults(List<WebSearchResponse> searchAll){
        Persist p = Persist.getSingleton();
        for (int i = 0; i < searchAll.size(); i++) {
            WebSearchResponse resp = searchAll.get(i);
            if(resp.getFile() != null){
                System.out.println(resp.getServiceName() + "["+resp.getSearchPosition()+
                    "]: " + resp.getTitle() + "\t\t\t\t\t File: "+resp.getFile().getAbsolutePath());
            } else {
                System.out.println(resp.getServiceName() + "["+resp.getSearchPosition()+
                    "]: " + resp.getTitle() + "\t\t\t\t\t File: is NULL");
            }
        }
    }
    
    private static void persistResults(List<WebSearchResponse> searchAll){
        Persist p = Persist.getSingleton();
        for (int i = 0; i < searchAll.size(); i++) {
            WebSearchResponse resp = searchAll.get(i);
            ClassificationStorage cs = new ClassificationStorage();
            cs.setDocumentType(DocumentType.UNKNOWN);
            cs.setWebSearchResponse(resp);
            System.out.println("Store: "+resp.getUrl());
            p.remoteObjects(cs);
        }
    }
}
